App.controller('bookingDateController', ['$rootScope', '$scope', '$state', '$window', 'Page',function($rootScope, $scope, $state, $window, Page) {
		if(!$scope.booking || !$scope.booking.plan){
			return false;
		}
		
		$scope.initMonthCalendar = function(Y,M,bookableDays,defaultDays){
			jsonstr = "[]";
			jsonarray = eval('(' + jsonstr + ')');
			var calendarNum = 1;
			var calendarRow = 0;
			var calendarLis = "<table class='order_calendar'>";
			calendarLis += "<tr><th colspan='7'>" + Y + '年' + M + '月' + "</th></tr>";
			calendarLis += "<tr><th>周日</th><th>周一</th><th>周二</th><th>周三</th><th>周四</th><th>周五</th><th>周六</th></tr>";
			
			drawCld(Y,(M-1));
			
			for (var i = 0; i < jsonarray.length; i++) {
				var className = "undays";//
				var element = jsonarray[i];
				var seating = null;
			   
			   if(i>7 && element.solar == ""){
				   continue;
			   }
				
			   if(calendarNum==1||calendarNum==8){
				   calendarNum = 1;
				   calendarRow ++;
				   calendarLis += '<tr>';  
			   }
			   
			   if(i<8 && element.solar == ""){
				   calendarLis += '<td></td>';
				   calendarNum++;
				   continue;
			   }
		      
			   $(bookableDays).each(function(i,date){ 
					if(date.date == element.day){
						seating = date.seating || 0;
						if(seating != 0 && seating != '0'){
							className = "days";
						}
						return false;
					}
			   });
			   
			   $(defaultDays).each(function(i,date){ 
					if((date.date && date.date == element.day) || date == element.day){
						className = className + ' ' + 'nac';
						return false;
					}
			   });
			   
			   calendarLis += '<td class="'+className+'"><input type="hidden" value="'+element.day+'"><font>'+element.solar+'</font><br/><font>'+element.lunar+'</font>' +
			   (seating != null ? ('<span>' + seating + '</span>') : '')
			   + '</td>';
		       
			   if(calendarNum==7){
				   calendarLis += '</tr>'; 
			   }
			   calendarNum++;
			}
			
			if(calendarNum < 8){
	        	for (var i = calendarNum; i < 8; i++) {
	        		calendarLis += '<td></td>';
				}
	        }
			if(calendarRow < 6){
				calendarLis += '<tr>';
	        	for (var i = 1; i < 8; i++) {
	        		calendarLis += '<td>&nbsp;</br>&nbsp;</td>';
				}
	        	calendarLis += '</tr>';
	        }
	        calendarLis += '</tr>';
	        calendarLis += '</table>';
	        return calendarLis;
		};
		
		$scope.initCalendar = function(startY,startM,endY,endM,bookableDays,defaultDays){
			$scope.calendars = [];
			var drawY = startY;
			var drawM = startM;
			while(drawY < endY || (drawY == endY && drawM <= endM)){
				$scope.calendars.push({
					html : $scope.initMonthCalendar(drawY,drawM,bookableDays,defaultDays)
				});
				drawY = drawM < 12 ? drawY : (drawY + 1);
				drawM = drawM < 12 ? (drawM + 1) : 1;
			}
		};
		
		$(document).off('click','.order_calendar .days');
		$(document).on('click','.order_calendar .days',function(e){
			e.preventDefault();	
			if($(this).hasClass("nac")){
				$(this).toggleClass("nac");
			}else{
				$(this).addClass("nac");
			}
		});
		
		
		$scope.today = new Date();
		$scope.currentY = $scope.today.getFullYear();
		$scope.currentM = $scope.today.getMonth() + 1;
		if($scope.currentM == 12){
			$scope.lastY = $scope.currentY + 1;
			$scope.lastM = 1;
		}else{
			$scope.lastY = $scope.currentY;
			$scope.lastM = $scope.currentM + 1;
		}
		
		//$scope.initCalendar(2017,07,2017,10, [],[]);
		
		Page.ajaxPost('linePlan/canBookDays.do',{planId : $scope.booking.plan.planId},function(resp){
			if(resp.data.length > 0){
				var lDate = resp.data[resp.data.length - 1].date;
				if(lDate){
					var lastDate = new Date(lDate);
					var lY = lastDate.getFullYear();
					var lM = lastDate.getMonth() + 1;
					$scope.lastY = lY > $scope.lastY ? lY : $scope.lastY;
					$scope.lastM = lY > $scope.lastY ? lM : (lM > $scope.lastM ? lM : $scope.lastM);
				}
			}
			$scope.initCalendar($scope.currentY,$scope.currentM,$scope.lastY,$scope.lastM, resp.data, $scope.booking.plan.planStatus == '0' ? resp.data : ($scope.booking.rideDates || []));
		},function(resp){
			$scope.initCalendar($scope.currentY,$scope.currentM,$scope.lastY,$scope.lastM, [],[]);
		});
		
		if($scope.booking.plan.planStatus == '0'){//待开-新线招募
			$(document).off('click','.order_calendar .days');
		}
		
		$scope.bookingNextStep = function() {
			$scope.booking.rideDates = [];
			$(".order_calendar .nac").each(function(i){ 
				$scope.booking.rideDates.push($(this).find('input').eq(0).val());
			});
			if($scope.booking.rideDates.length == 0){
				Page.alert("请选择乘车日期!");
				return;
			}
			$state.go('booking.passenger');
		};
		
		$scope.bookingPreStep = function() {
			$state.go('booking.plan');
		};
	
}]);